import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/main.dart';
import 'package:mirror/widget/CropWidget.dart';
import 'package:path_provider/path_provider.dart';

class Util {
  static toast(s) {
    Fluttertoast.showToast(msg: "$s");
  }

  static String timeFormat({required DateTime time}) {
    var now = DateTime.now();
    String resTime = "";
    if (time.day == now.day) {
      resTime =
          "${'${time.hour}'.padLeft(2, "0")}:${'${time.minute}'.padLeft(2, "0")}";
    } else if (time.day == now.subtract(const Duration(days: 1)).day) {
      resTime =
          "昨天${'${time.hour}'.padLeft(2, "0")}:${'${time.minute}'.padLeft(2, "0")}";
    } else if (time.day == now.add(const Duration(days: 1)).day) {
      resTime =
          "明天${'${time.hour}'.padLeft(2, "0")}:${'${time.minute}'.padLeft(2, "0")}";
    } else {
      DateTime startOfWeek = now
          .subtract(Duration(days: now.weekday - 1))
          .toUtc()
          .subtract(Duration(
              hours: now.hour,
              minutes: now.minute,
              seconds: now.second,
              milliseconds: now.millisecond,
              microseconds: now.microsecond));
      DateTime endOfWeek = startOfWeek.add(const Duration(days: 6)).add(
          const Duration(
              hours: 23,
              minutes: 59,
              seconds: 59,
              milliseconds: 999,
              microseconds: 999));

      if (time.isAfter(startOfWeek) && time.isBefore(endOfWeek)) {
        resTime = "周${time.weekday}";
      } else {
        if (time.year == now.year) {
          resTime =
              "${time.month}月${'${time.day}'.padLeft(2, "0")}日 ${'${time.hour}'.padLeft(2, "0")}:${'${time.minute}'.padLeft(2, "0")}";
        } else {
          resTime =
              "${time.year}年${time.month}月${'${time.day}'.padLeft(2, "0")}日 ${'${time.hour}'.padLeft(2, "0")}:${'${time.minute}'.padLeft(2, "0")}";
        }
      }
    }
    return resTime;
  }

  static void copy(String s) {
    Clipboard.setData(ClipboardData(text: s));
    toast("已复制");
  }

  static Future<Uint8List?> crop(Uint8List? data) async {
    if (data == null) {
      toast("选择图片出错");
      return null;
    }
    var res = await Navigator.push(navigatorKey.currentContext!,
        MaterialPageRoute(builder: (c) => CropWidget(data: data)));
    return res;
  }

  static Future<void> saveFile(
      {required String path, required Uint8List data}) async {
    var file = File(path);
    file.writeAsBytesSync(data);
  }

  static Future<String> getTmpPath() async {
    Directory dir = await getTemporaryDirectory();
    if (!dir.existsSync()) {
      dir.createSync();
    }
    return "${dir.path}/";
  }

  static Future<String?> showBottomInputDialog(
      {required String title, required String hint}) async {
    var controller = TextEditingController();
    return await showModalBottomSheet(
        isScrollControlled: true,
        context: navigatorKey.currentContext!,
        builder: (c) => StatefulBuilder(
              builder: (context, state) {
                return AnimatedPadding(
                  padding: MediaQuery.of(context).viewInsets,
                  duration: Duration.zero,
                  child: SingleChildScrollView(
                      child: Column(
                    children: [
                      Align(
                        alignment: Alignment.centerRight,
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: const Padding(
                            padding: EdgeInsets.all(8.0),
                            child: Icon(Icons.close),
                          ),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          title,
                          style: const TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 15),
                              child: Container(
                                padding: const EdgeInsets.symmetric(
                                    vertical: 15, horizontal: 10),
                                decoration: BoxDecoration(
                                    borderRadius: Constant.borderRadius,
                                    color: Colors.white,
                                    boxShadow: Constant.boxShadow),
                                child: TextField(
                                  controller: controller,
                                  decoration:
                                      InputDecoration.collapsed(hintText: hint),
                                ),
                              ),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            Align(
                              alignment: Alignment.bottomRight,
                              child: InkWell(
                                onTap: () {
                                  if (controller.text.isEmpty) {
                                    toast(hint);
                                    return;
                                  }
                                  Navigator.pop(context, controller.text);
                                },
                                child: Container(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 20, vertical: 10),
                                  decoration: BoxDecoration(
                                      color: Constant.mainColor,
                                      borderRadius: Constant.borderRadius),
                                  child: const Text(
                                    "确定",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                    ],
                  )),
                );
              },
            ));
  }

  static showBottomLabelDialog({required List lists}) async {
    List labels = lists;
    var controller = TextEditingController();
    return await showModalBottomSheet(
        isScrollControlled: true,
        context: navigatorKey.currentContext!,
        builder: (c) => StatefulBuilder(
              builder: (context, state) {
                return AnimatedPadding(
                  padding: MediaQuery.of(context).viewInsets,
                  duration: Duration.zero,
                  child: SingleChildScrollView(
                      child: Column(
                    children: [
                      Align(
                        alignment: Alignment.centerRight,
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: const Padding(
                            padding: EdgeInsets.all(8.0),
                            child: Icon(Icons.close),
                          ),
                        ),
                      ),
                      const Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Text(
                          "请设置爱好",
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      labels.isEmpty
                          ? const Text(
                              "请输入后添加",
                              style: TextStyle(fontSize: 13),
                            )
                          : Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 5),
                              child: Container(
                                decoration: BoxDecoration(
                                    border:
                                        Border.all(color: Constant.mainColor),
                                    borderRadius: Constant.borderRadius),
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 10, vertical: 10),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Wrap(
                                      spacing: 10,
                                      children: List.generate(
                                          labels.length,
                                          (index) => InkWell(
                                                onTap: () {
                                                  labels.removeAt(index);
                                                  state(() {});
                                                },
                                                child: Padding(
                                                  padding: const EdgeInsets
                                                      .symmetric(vertical: 5),
                                                  child: Container(
                                                    padding: const EdgeInsets
                                                            .symmetric(
                                                        horizontal: 20,
                                                        vertical: 5),
                                                    decoration: BoxDecoration(
                                                        color:
                                                            Constant.mainColor,
                                                        borderRadius: Constant
                                                            .borderRadius),
                                                    child: Text(
                                                      labels[index],
                                                      style: const TextStyle(
                                                          color: Colors.white,
                                                          fontSize: 10),
                                                    ),
                                                  ),
                                                ),
                                              )),
                                    ),
                                    const SizedBox(
                                      height: 10,
                                    ),
                                    Text(
                                      "当前爱好数量:${labels.length}(点击可删除)",
                                      style: TextStyle(
                                          color: Constant.mainColor,
                                          fontSize: 10),
                                    )
                                  ],
                                ),
                              ),
                            ),
                      const SizedBox(
                        height: 15,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          children: [
                            Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 15),
                              child: Row(
                                children: [
                                  Expanded(
                                    child: Container(
                                      padding: const EdgeInsets.symmetric(
                                          vertical: 15, horizontal: 10),
                                      decoration: BoxDecoration(
                                          borderRadius: Constant.borderRadius,
                                          color: Colors.white,
                                          boxShadow: Constant.boxShadow),
                                      child: TextField(
                                        controller: controller,
                                        maxLength: 5,
                                        style: const TextStyle(fontSize: 13),
                                        decoration:
                                            const InputDecoration.collapsed(
                                                hintText: "请输入爱好",
                                                hintStyle:
                                                    TextStyle(fontSize: 13)),
                                      ),
                                    ),
                                  ),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  InkWell(
                                    onTap: () {
                                      if (controller.text.isEmpty) {
                                        toast("请输入爱好");
                                        return;
                                      }
                                      if (labels.length > 14) {
                                        toast("最多添加15个");
                                        return;
                                      }
                                      labels.add(controller.text);
                                      controller.clear();
                                      state(() {});
                                    },
                                    child: Container(
                                      padding: const EdgeInsets.all(13),
                                      decoration: BoxDecoration(
                                          shape: BoxShape.circle,
                                          color: Colors.white,
                                          boxShadow: Constant.boxShadow),
                                      child: const Icon(Icons.add),
                                    ),
                                  )
                                ],
                              ),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            Align(
                              alignment: Alignment.bottomRight,
                              child: InkWell(
                                onTap: () {
                                  Navigator.pop(context, labels);
                                },
                                child: Container(
                                  padding: const EdgeInsets.symmetric(
                                      horizontal: 20, vertical: 10),
                                  decoration: BoxDecoration(
                                      color: Constant.mainColor,
                                      borderRadius: Constant.borderRadius),
                                  child: const Text(
                                    "确定",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                    ],
                  )),
                );
              },
            ));
  }
}
